<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="110px" :rules="dynamicFormRules">
                <el-form-item label="短剧标识" prop="drama_id">
                    <el-input v-model="formData.drama_id" disabled placeholder="请输入短剧唯一标识" />
                </el-form-item>
                <el-form-item label="剧集标识" v-show="isEditShow" prop="series_id">
                    <el-input v-model="formData.series_id" disabled placeholder="请输入剧集唯一标识" />
                </el-form-item>
                <el-form-item label="短剧标题" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入短剧标题" />
                </el-form-item>
                <el-form-item label="剧集内容" prop="video_url">
                    <material-picker type="video" v-model="formData.video_url" :limit="1" />
                </el-form-item>
                <el-form-item label="是否需要付费" prop="is_vip">
                    <div>
                        <el-switch v-model="formData.is_vip" :active-value="1" :inactive-value="0" />
                    </div>
                </el-form-item>
                <el-form-item label="序号" v-show="isEditShow" prop="sort">
                    <el-input v-model="formData.sort" type="number" placeholder="请输入序号" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  seriesEdit, seriesAdd, seriesDetail } from '@/api/client/series'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const { query } = useRoute()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑短剧剧集' : '新增短剧剧集'
})

const isEditShow = computed(() => mode.value === 'edit')

const formData = reactive({
    series_id: '',
    drama_id: String(query.drama_id),
    title: '',
    video_url: '',
    is_vip: 0,
    sort: 0,
})

const formRules = {
    series_id: [
        {
            required: true,
            message: '请输入剧集唯一标识',
            trigger: ['blur']
        }
    ],
    drama_id: [
        {
            required: true,
            message: '请输入短剧唯一标识',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: false,
            message: '请输入短剧标题',
            trigger: ['blur']
        }
    ],
    video_url: [
        {
            required: true,
            message: '请输入封面图URL',
            trigger: ['blur']
        }
    ],
    is_vip: [
        {
            required: true,
            message: '请输入1需付费 0免费',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入序号',
            trigger: ['blur']
        }
    ],
}
// 修改为动态规则
const dynamicFormRules = computed(() => {
    const rules = { ...formRules }

    // 在添加模式下，series_id 不需要必填
    if (mode.value === 'add') {
        rules.series_id = [
            {
                required: false,
                message: '请输入剧集唯一标识',
                trigger: ['blur']
            }
        ]
        rules.sort = [
            {
                required: false,
                message: '请输入剧集唯一标识',
                trigger: ['blur']
            }
        ]
    }

    return rules
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    // 确保 sort 是整数类型
    if (data.sort !== '') {
        data.sort = parseInt(data.sort) || 0
    }
    mode.value == 'edit' ? await seriesEdit(data) : await seriesAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await seriesDetail({
        series_id: row.series_id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
